package com.supermap.search.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.supermap.greendao.XunJianOneTimeDao;
import com.supermap.search.R;
import com.supermap.search.adapter.SwipeXunJianAdapter;
import com.supermap.search.javabean.XunJianOneTime;
import com.supermap.search.sys.loc.LocationApplication;
import com.supermap.search.utils.ToastUtils;
import com.supermap.search.utils.Utils;
import com.supermap.search.wigdet.SwipeLayoutManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 未上传巡检的界面
 * Created by XueQing Wang on 2018/6/29.
 */

public class NoUpXunJianActivity extends AppCompatActivity {
    @BindView(R.id.tv_allclear)
    TextView tvAllclear;
    @BindView(R.id.lv_weishangchuan)
    ListView lvWeishangchuan;


    //初始化巡检dao
    private XunJianOneTimeDao dao;
    private List<XunJianOneTime> xunJianOneTimes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_noshangchuanxunjian);
        ButterKnife.bind(this);
        initdata();
    }

    private void initdata() {
        dao = LocationApplication.getInstance().getDaoSession().getXunJianOneTimeDao();
        xunJianOneTimes = dao.loadAll();
        for (int i = 0; i < xunJianOneTimes.size(); i++) {
            XunJianOneTime xunJianOneTime = xunJianOneTimes.get(i);
            String state = xunJianOneTime.getState();
            if (!"1".equals(state)) {
//                xunJianOneTimes.remove(i);
            }
        }
        if (xunJianOneTimes != null && xunJianOneTimes.size() > 0) {
            //不為空
            lvWeishangchuan.setVisibility(View.VISIBLE);
            tvAllclear.setVisibility(View.GONE);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    SwipeXunJianAdapter adapter = new SwipeXunJianAdapter(xunJianOneTimes, Utils.getContext());
                    adapter.setOnClickListener(new SwipeXunJianAdapter.OnClickListener() {
                        @Override
                        public void onUploadClick(int position) {

                        }

                        @Override
                        public void onDeleteClick(int position) {
                            dao.delete(xunJianOneTimes.get(position));
                            xunJianOneTimes.remove(position);
                            adapter.notifyDataSetChanged();
                            ToastUtils.showShortToast("删除成功");
                        }

                        @Override
                        public void onInfoClick(int position) {
                            Intent intent = new Intent(NoUpXunJianActivity.this, WenTiXiangQingActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("user", xunJianOneTimes.get(position));
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                    lvWeishangchuan.setAdapter(adapter);
                }
            }).start();

        } else

        {
            lvWeishangchuan.setVisibility(View.GONE);
            tvAllclear.setVisibility(View.VISIBLE);
        }

    }

    @Override
    protected void onPause() {
        SwipeLayoutManager.getInstance().clearCurrentLayout();
        super.onPause();
    }
}
